package com.zuoshen_algorithm;

import java.util.stream.Stream;

/**
 * 插入排序(从小到达)
 * 时间复杂度最坏（O(n2)）
 *
 */
public class ChaRuSort {

    public static void main(String[] args) {
        Integer[] num = {4, 1, 2, 7, 8, 5, 3};
        Stream.of(num).forEach(x -> System.out.print(x));
        System.out.println();
        sort(num);
        Stream.of(num).forEach(x -> System.out.print(x));
    }

    public static void sort(Integer[] num) {
        if (num == null || num.length < 2) {
            return;
        }
        for (int i = 1; i < num.length; i++) {
            for (int j = i - 1; j >= 0 && num[j] > num[j + 1]; j--) {
                Util.swap(num, j, j + 1);
            }
        }
    }
}
